package handler

import (
	"0411zk/api/client"
	"0411zk/api/model"
	study "0411zk/common/proto"
	"0411zk/pkg"
	"github.com/gin-gonic/gin"
	"net/http"
)

func UserRegister(c *gin.Context) {
	var data model.UserRegister
	if err := c.ShouldBind(&data); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 0,
			"msg":  err.Error(),
			"data": nil})
		return
	}
	register, err := client.UserRegister(c, &study.UserRegisterReq{
		Mobile:   data.Mobile,
		Password: data.Password,
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 0,
			"msg":  err.Error(),
			"data": nil})
		return
	}
	claims := pkg.CustomClaims{
		ID: uint(register.UserId),
	}
	token, err := pkg.NewJWT("2209a").CreateToken(claims)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 0,
			"msg":  "token生成错误",
			"data": nil})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "新用户注册成功,优惠卷已发放",
		"data": token})
}

func BrowseCourses(c *gin.Context) {
	register, err := client.BrowseCourses(c, &study.BrowseCoursesReq{})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 0,
			"msg":  err.Error(),
			"data": nil})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "课程信息展示成功",
		"data": register})
}

func BrowseCoursesAdd(c *gin.Context) {
	var data model.BrowseCoursesAdd
	if err := c.ShouldBind(&data); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 0,
			"msg":  err.Error(),
			"data": nil})
		return
	}
	id := c.GetUint("userId")
	register, err := client.BrowseCoursesAdd(c, &study.BrowseCoursesAddReq{
		CoursesId: int64(data.CoursesId),
		UserId:    int64(id),
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 0,
			"msg":  err.Error(),
			"data": nil})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "学习计划课程添加成功",
		"data": register})
}

func BrowseCoursesDel(c *gin.Context) {
	var data model.BrowseCoursesDel
	if err := c.ShouldBind(&data); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 0,
			"msg":  err.Error(),
			"data": nil})
		return
	}
	id := c.GetUint("userId")
	register, err := client.BrowseCoursesDel(c, &study.BrowseCoursesDelReq{
		CoursesId: int64(data.CoursesId),
		UserId:    int64(id),
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 0,
			"msg":  err.Error(),
			"data": nil})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "学习计划课程删减成功",
		"data": register})
}

func CoursesOrder(c *gin.Context) {
	var data model.CoursesOrder
	if err := c.ShouldBind(&data); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 0,
			"msg":  err.Error(),
			"data": nil})
		return
	}
	id := c.GetUint("userId")
	register, err := client.CoursesOrder(c, &study.CoursesOrderReq{
		CoursesId: int64(data.CoursesId),
		Sum:       int64(data.Sum),
		Pay:       int64(data.Pay),
		UserId:    int64(id),
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 0,
			"msg":  err.Error(),
			"data": nil})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "课程订单创建成功",
		"data": register})
}
